/*
 * Copyright (c) Thorben Linneweber and others
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

using System;
using Jitter2.LinearMath;

using SoftFloat;

namespace Jitter2.Collision.Shapes
{
	
	/// <summary>
	/// Represents a cylinder shape.
	/// </summary>
	public class CylinderShape : RigidBodyShape
	{
	    private sfloat radius;
	    private sfloat height;
	
	    /// <summary>
	    /// Gets or sets the radius of the cylinder.
	    /// </summary>
	    public sfloat Radius
	    {
	        get => radius;
	        set
	        {
	            radius = value;
	            UpdateWorldBoundingBox(sfloat.Zero);
	        }
	    }
	
	    /// <summary>
	    /// Gets or sets the height of the cylinder.
	    /// </summary>
	    public sfloat Height
	    {
	        get => height;
	        set
	        {
	            height = value;
	            UpdateWorldBoundingBox(sfloat.Zero);
	        }
	    }
	
	    /// <summary>
	    /// Initializes a new instance of the <see cref="CylinderShape"/> class, creating a cylinder shape with the specified height and radius. The symmetry axis of the cylinder is aligned along the y-axis.
	    /// </summary>
	    /// <param name="height">The height of the cylinder.</param>
	    /// <param name="radius">The radius of the cylinder at its base.</param>
	    public CylinderShape(sfloat height, sfloat radius)
	    {
	        this.radius = radius;
	        this.height = height;
	        UpdateWorldBoundingBox(sfloat.Zero);
	    }
	
	    public override void GetCenter(out NVector3 point)
	    {
	        point = centerOffset;
	    }
	
	    public override void SupportMap(in NVector3 direction, out NVector3 result)
	    {
	        sfloat sigma = (sfloat)libm.Sqrt(direction.X * direction.X + direction.Z * direction.Z);
	
	        if (sigma > (sfloat)0.0f)
	        {
	            result.X = direction.X / sigma * radius;
	            result.Y = libm.Sign(direction.Y) * height * (sfloat)0.5f;
	            result.Z = direction.Z / sigma * radius;
	        }
	        else
	        {
	            result.X = (sfloat)0.0f;
	            result.Y = libm.Sign(direction.Y) * height * (sfloat)0.5f;
	            result.Z = (sfloat)0.0f;
	        }
	    
	        result += centerOffset;// 应用中心偏移
		}
	
	    public override void CalculateBoundingBox(in NQuaternion orientation, in NVector3 position, out JBBox box)
	    {
	        sfloat zeroEpsilon = 1e-12f;
	
	        NVector3 upa = orientation.GetBasisY();
	
	        sfloat xx = upa.X * upa.X;
	        sfloat yy = upa.Y * upa.Y;
	        sfloat zz = upa.Z * upa.Z;
	
	        sfloat l1 = yy + zz;
	        sfloat l2 = xx + zz;
	        sfloat l3 = xx + yy;
	
	        sfloat xext = 0, yext = 0, zext = 0;
	
	        if (l1 > zeroEpsilon)
	        {
	            sfloat sl = (sfloat)1.0f / libm.Sqrt(l1);
	            xext = (yy + zz) * sl * radius;
	        }
	
	        if (l2 > zeroEpsilon)
	        {
	            sfloat sl = (sfloat)1.0f / libm.Sqrt(l2);
	            yext = (xx + zz) * sl * radius;
	        }
	
	        if (l3 > zeroEpsilon)
	        {
	            sfloat sl = (sfloat)1.0f / libm.Sqrt(l3);
	            zext = (xx + yy) * sl * radius;
	        }
	
	        NVector3 p1 = -(sfloat)0.5f * height * upa;
	        NVector3 p2 = +(sfloat)0.5f * height * upa;
	
	        NVector3 delta = NVector3.Max(p1, p2) + new NVector3(xext, yext, zext);
	
	        box.Min = position - delta;
	        box.Max = position + delta;
	    
	        // 考虑位置和中心偏移
	        box.Min += centerOffset;
	        box.Max += centerOffset;
		}
	
	    public override void CalculateMassInertia(out JMatrix inertia, out NVector3 com, out sfloat mass)
	    {
	        mass = libm.PI * radius * radius * height;
	
	        inertia = JMatrix.Identity;
	
	        inertia.M11 = (sfloat)1.0f / (sfloat)4.0f * mass * radius * radius + (sfloat)1.0f / (sfloat)12.0f * mass * height * height;
	        inertia.M22 = (sfloat)1.0f / (sfloat)2.0f * mass * radius * radius;
	        inertia.M33 = (sfloat)1.0f / (sfloat)4.0f * mass * radius * radius + (sfloat)1.0f / (sfloat)12.0f * mass * height * height;
	
	        com = NVector3.Zero;
	    }
	}
}
